﻿Imports System.Data.SqlClient
Public Class frm_dangkylop

    Dim tbcbomakhoa As New DataTable
    Sub loadcbomakhoa()
        Dim cmd As New SqlCommand("sp_formdangkylop_loadcbokhoahoc", conn)
        cmd.CommandType = CommandType.StoredProcedure

        Dim da As New SqlDataAdapter(cmd)

        da.Fill(tbcbomakhoa)

        cbokhoahoc.DataSource = tbcbomakhoa
        cbokhoahoc.DisplayMember = "TenKhoa"
        cbokhoahoc.ValueMember = "MaKhoa"
    End Sub

    Private Sub frm_dangkylop_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        conn.Open()
        loadcbomakhoa()
        dgvthongtin.Visible = False

    End Sub

    Private Sub cbokhoahoc_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbokhoahoc.SelectedIndexChanged
        Try
            cbolop.Text = ""
            dgvdanhsach.Columns.Clear()
            txthocphi.Text = ""
            txtlop.Text = ""
            txtmonhoc.Text = ""
            Dim cmd As New SqlCommand("sp_formdangkylop_loadcbolop", conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@makhoa", cbokhoahoc.SelectedValue)

            Dim da As New SqlDataAdapter(cmd)
            Dim tbcbolop As New DataTable

            da.Fill(tbcbolop)

            cbolop.DataSource = tbcbolop
            cbolop.DisplayMember = "TenLop"
            cbolop.ValueMember = "MaLop"
        Catch ex As Exception

        End Try
    End Sub
    Sub loaddanhsach()
        Dim cmd As SqlCommand
        Dim da As SqlDataAdapter
        Dim ds As New DataSet
        cmd = New SqlCommand("sp_formdangkylop_loaddgvdanhsach", conn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@malop", cbolop.SelectedValue)

        da = New SqlDataAdapter(cmd)

        da.Fill(ds, "DanhSach")

        dgvdanhsach.DataSource = ds.Tables("DanhSach")
    End Sub
    Private Sub cbolop_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbolop.SelectedIndexChanged
        Try
            Dim cmd As SqlCommand
            Dim da As SqlDataAdapter
            Dim ds As New DataSet

            'load dgvdanhsach
            cmd = New SqlCommand("sp_formdangkylop_loaddgvdanhsach", conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@malop", cbolop.SelectedValue)

            da = New SqlDataAdapter(cmd)

            da.Fill(ds, "DanhSach")

            dgvdanhsach.DataSource = ds.Tables("DanhSach")

            'load thong tin lop
            cmd = New SqlCommand("sp_formdangkylop_loadthongtinlop", conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@malop", cbolop.SelectedValue)

            da = New SqlDataAdapter(cmd)
            da.Fill(ds, "Thongtin")

            dgvthongtin.DataSource = ds.Tables("Thongtin")

            Dim cl As DataGridViewRow = dgvthongtin.CurrentRow
            txtlop.Text = cl.Cells.Item("TenLop").Value
            txtmonhoc.Text = cl.Cells.Item("TenMon").Value
            txthocphi.Text = cl.Cells.Item("HocPhi").Value
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmddangky_Click(sender As Object, e As EventArgs) Handles cmddangky.Click
        Try
            Dim cmd As New SqlCommand("sp_formdangkylop_insertdangkylop", conn)
            cmd.CommandType = CommandType.StoredProcedure

            cmd.Parameters.AddWithValue("@mahocvien", txtmahocvien.Text)
            cmd.Parameters.AddWithValue("@malop", cbolop.SelectedValue)

            Dim i As Integer = cmd.ExecuteNonQuery

            If i > 0 Then
                MsgBox("Đăng ký lớp thành công", Title:="Thông báo!")
                loaddanhsach()
            End If
        Catch ex As Exception
            MsgBox("Học viên đã đăng ký rồi!")
        End Try
    End Sub

    Private Sub cmdhuy_Click(sender As Object, e As EventArgs) Handles cmdhuy.Click
        Try
            Dim cmd As New SqlCommand("sp_formdangkylop_deletedangkylop", conn)
            cmd.CommandType = CommandType.StoredProcedure

            cmd.Parameters.AddWithValue("@mahocvien", txtmahocvien.Text)
            cmd.Parameters.AddWithValue("@malop", cbolop.SelectedValue)

            Dim i As Integer = cmd.ExecuteNonQuery

            If i > 0 Then
                MsgBox("Hủy đăng ký lớp thành công", Title:="Thông báo!")
                loaddanhsach()
            End If
        Catch ex As Exception
            MsgBox("Không có học viên này trong lớp!")
        End Try
    End Sub
End Class